package com.ztesoft.web.taskscheduling.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.core.cache.EhCacheService;
import com.ztesoft.core.spring.context.SpringApplicationContextHolder;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.iocp.utils.Utils;
import com.ztesoft.web.baseconfig.db.arg.DmDataStateArg;
import com.ztesoft.web.baseconfig.db.dao.DmDataStateDao;
import com.ztesoft.web.baseconfig.db.po.DmDataStatePO;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.db.arg.DmArg;
import com.ztesoft.web.common.db.dao.DmDao;
import com.ztesoft.web.common.service.IOperationLogService;
import com.ztesoft.web.common.vo.TaskNotify;
import com.ztesoft.web.taskgenerator.db.arg.DmTaskStageArg;
import com.ztesoft.web.taskgenerator.db.dao.DmTaskDao;
import com.ztesoft.web.taskgenerator.db.dao.DmTaskStageDao;
import com.ztesoft.web.taskgenerator.db.po.DmTaskPO;
import com.ztesoft.web.taskgenerator.db.po.DmTaskStagePO;
import com.ztesoft.web.taskscheduling.service.SystemStartInitInfoService;

@Service
public class SystemStartInitInfoServiceImpl implements SystemStartInitInfoService{

	/**
	 * 日志
	 */
	private final static ZTEsoftLogManager LOGGER = ZTEsoftLogManager
			.getLogger(SystemStartInitInfoServiceImpl.class);

	@Autowired
	private DmTaskDao dmTaskDao;
	
	@Autowired
	private DmTaskStageDao dmTaskStageDao;
	
	@Autowired
	private IOperationLogService operationLogService;

	@Autowired
	private DmDao dmDao;
	
	@Autowired
	private DmDataStateDao dmDataStateDao;
	
	@Resource(name = "frameworkEhCacheService")
	private EhCacheService dmsCacheService;
	
	@Override
	public void taskManage() {
		//后台应用启动时，先加载计划的数据恢复状态 map<Integer, String> dataRestore
		setDataRestore();
		
		DmTaskStageArg arg = new DmTaskStageArg();
		arg.createCriteria().andStateEqualTo("00R");
		List<DmTaskStagePO> stageLst = dmTaskStageDao.selectByArg(arg);
		
		for (DmTaskStagePO dmTaskStagePO : stageLst) {
			if(1==dmTaskStagePO.getDataType()){
				//根据执行环节进行处理
				try {
					updateDateHandleService(dmTaskStagePO);
				} catch (BaseAppException e) {
					e.printStackTrace();
				}
			}else{
				//文件类型，暂不处理
			}
		}
		// 应用启动时获取系统配置相关参数：
	}
		/**
		 * 后台应用启动时，先加载计划的数据恢复状态 map<Integer, String> dataRestore
		 */
		private void setDataRestore() {
			DmDataStateArg stateArg = new DmDataStateArg();
			List<DmDataStatePO> stateLst = dmDataStateDao.selectByArg(stateArg);
			if(Utils.notEmpty(stateLst)){
				for (DmDataStatePO statePO : stateLst) {
					//DMSConstant.dataRestore.put(statePO.getDmPlanId(),statePO.getState());
					dmsCacheService.put("dmsCache", "dataRecovery_"+statePO.getDmPlanId(), statePO.getState());
					System.out.println("dataRecovery_"+statePO.getDmPlanId()+","+dmsCacheService.get("dmsCache", "dataRecovery_"+statePO.getDmPlanId()));
				}
			}
		
		
		}

	//加入操作日志表
	private void updateDateHandleService(DmTaskStagePO dmTaskStagePO) throws BaseAppException{
		DmTaskStagePO dmTaskStage =  new DmTaskStagePO();
		DmTaskStageArg stageArg = new DmTaskStageArg();
		DmTaskStageArg.DmTaskStageCriteria stagec = stageArg.createCriteria();
		dmTaskStage.setState("00T");
		dmTaskStage.setStateDate(new Date());
		stagec.andDmInfoIdEqualTo(dmTaskStagePO.getDmInfoId());

		DmTaskPO dmTask = new DmTaskPO();
		dmTask.setState("00T");
		dmTask.setStateDate(new Date());
		dmTask.setDmTaskId(dmTaskStagePO.getDmTaskId());
		String operStr = "";
		if(dmTaskStagePO.getStage()==0){
			//1.1若环节为体检即STAGE=0，进行以下处理
			//A、删除体检结果数据：
			//B、更新任务环节表对应记录的“结果数据量”字段为0：
			//C、更新任务实例状态为待执行：
			
			
			DmArg dmarg = new DmArg();
			DmArg.DmCriteria criteria = dmarg.createCriteria();
		    criteria.andDmTaskIdEqualTo(dmTaskStagePO.getDmTaskId());
		    criteria.andDmStrategyIdEqualTo(dmTaskStagePO.getDmStrategyId());
		    //criteria.andStateEqualTo("00B");
		    dmDao.deleteByArg("DM_"+dmTaskStagePO.getCheckObjName(), dmarg);

			operStr = "DELETE FROM "+ "DM_"+dmTaskStagePO.getCheckObjName()+ " WHERE DM_TASK_ID="+dmTaskStagePO.getDmTaskId();
			operationLogService.addOperationLogPO(2, operStr);
			
			dmTaskStage.setErrorDesc("重启");
			dmTaskStage.setCheckCount(0);
			dmTaskStage.setTaskStartTime(null);
			dmTaskStage.setTaskEndTime(null);
			dmTaskStage.setTotalRecordCount(0);
			dmTaskStage.setTotalDataSize(java.math.BigDecimal.ZERO);
			dmTaskStage.setCheckDataSize(java.math.BigDecimal.ZERO);
			stagec.andStageEqualTo(0);
			
			
		}else if(dmTaskStagePO.getStage()==1){
			//1.2若环节为备份即STAGE=1，进行以下处理
			//A、更新任务环节的状态为待执行：
			//B、更新任务实例状态为待执行：
			stagec.andStageEqualTo(1);
			
		}else{
			//1.3若环节为清理即STAGE=2，进行以下处理
			//A、更新任务环节的状态为待执行：
			//B、更新任务实例状态为待执行：
			stagec.andStageEqualTo(2);
			
		}
		dmTaskStageDao.updateByArgSelective(dmTaskStage, stageArg);
		operStr = "UPDATE DM_TASK_STAGE SET CHECK_COUNT=0 WHERE DM_INFO_ID="+dmTaskStagePO.getDmInfoId()+ "AND STAGE="+dmTaskStage.getState();
		operationLogService.addOperationLogPO(1, operStr);
		dmTaskDao.updateByPrimaryKeySelective(dmTask);
		LOGGER.info("系统重启是更新任务表状态为待执行"+dmTask);
		operStr = "UPDATE DM_TASK SET STATE=’00T’ WHERE DM_TASK_ID="+dmTaskStagePO.getDmTaskId();
		operationLogService.addOperationLogPO(1, operStr);
		
	}
}
